﻿using System;
using System.Collections;
using System.Data;
using WCF_BI_Portal.GeneralLibs;

namespace WCF_BI_Portal.Lists
{
    public class User_Database : DoiTuong
    {
        #region Guid UserID property
        public Guid UserID
        {
            get
            {
                if (_DataRow["UserID"] is Guid) return (Guid)_DataRow["UserID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["UserID"] = value;
            }
        }
        #endregion

        #region Guid DataBaseID property
        public Guid DataBaseID
        {
            get
            {
                if (_DataRow["DataBaseID"] is Guid) return (Guid)_DataRow["DataBaseID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["DataBaseID"] = value;
            }
        }
        #endregion

        public User_Database(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucUser_Database : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucUser_Database _Instance;
        public static DanhMucUser_Database Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucUser_Database();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    //_Instance.DataView.Sort = "Ma";
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "User_DataBase";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new User_Database((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public User_Database this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new User_Database(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public User_Database NewDoiTuong()
        {
            return new User_Database(DataTable.NewRow());
        }

        public User_Database GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new User_Database(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public User_Database GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucUser_Database GetAll()
        {
            return Instance;
        }

        public static DanhMucUser_Database GetByUserID(Guid userID)
        {
            return Filter(string.Format("UserID = '{0}'", userID));
        }

        public static DanhMucUser_Database GetByDataBaseID(Guid dbID)
        {
            return Filter(string.Format("DataBaseID = '{0}'", dbID));
        }

        public static DanhMucUser_Database GetByUserID_DataBaseID(Guid userID, Guid dbID)
        {
            return Filter(string.Format("UserID = '{0}' AND DataBaseID = '{1}'", userID, dbID));
        }

        public static DanhMucUser_Database Filter(string filterExpression)
        {
            try
            {
                DanhMucUser_Database obj = new DanhMucUser_Database();
                obj.DataView = new DataView(DanhMucUser_Database.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}